import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {//仓库中的数据，相当于vue组件中的data返回对象
    goodsList: [],
  },
  getters: {//计算属性
    selectedGoods (state) {
      let arr = []
      state.goodsList.forEach(good => {
        good.foods.forEach(food => {
          if (food.count > 0) {
            arr.push(food)
          }
        })
      })
      let newArr = []
      arr.forEach(item => {
        if (newArr.every(v => v.name !== item.name)){ 
          newArr.push(item)
        }
      })
      // newArr.reduce((res, cur) => {
      //   if (res.every(v => v.name != cur.name)) {
      //     res.push(cur)
      //   }
      //   return res;
      // }, [])
      return newArr
    },

  },
  mutations: {//方法同步
    ADD_GOODS (state, goods) {
      console.log('state,goods :>> ', state, goods);
      state.goodsList = goods
    }
  },
  actions: {//方法异步
  },
  modules: {//模块
  }
})
